package com.todod.base.module.base.util;

import cn.hutool.http.HttpUtil;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;

/**
 * <p>
 * 服务类
 * </p>
 *
 * @author gsh
 * @since 2020-10-14
 */
@Service
public class RemoteService {
	private static Logger log = LoggerFactory.getLogger(RemoteService.class);
	@Resource
	private RestTemplate restTemplate;
	@Value("${remoteUrl}")
    private String remoteUrl;

	/**
	 * @title sendBaseInfoByCode
	 * @param code
	 * @param type
	 * @return
	 */
	public String sendBaseInfoByCode(String code,int type) {

		HashMap<String, Object> paramMap = new HashMap<>();
		String result= "";
		try{
			result= HttpUtil.post(remoteUrl+"/sendBaseInfoByCode?code="+code+"&type="+type, paramMap);
		} catch (Exception e){
			log.info("send-error===={}", e);
		}

//		HttpHeaders headers = new HttpHeaders();
//		headers.setContentType(MediaType.APPLICATION_JSON);
//		HttpEntity httpEntity = new HttpEntity<>(headers);
//		String s = remoteUrl + "/sendBaseInfoByCode?code=" + code + "&type=" + type;
//		ResponseEntity<String> postForEntity = restTemplate.postForEntity(remoteUrl+"/sendBaseInfoByCode?code="+code+"&type="+type, httpEntity, String.class);
//		log.info("send-success===={}", postForEntity.getBody());
		log.info("send-success===={}", result);
		return result;
	}

	/**
	 * @title sendBaseInfoByCode
	 * @param code
	 * @param type
	 * @return
	 */
	public String sendBaseInfoByCode(int port) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		HttpEntity httpEntity = new HttpEntity<>(headers);
		ResponseEntity<String> postForEntity = restTemplate.postForEntity(remoteUrl+"/sendBaseInfoByCode?code="+port, httpEntity, String.class);
		System.out.println(postForEntity.getBody());
		JSONObject reData = JSONObject.parseObject(postForEntity.getBody());
		String reName = reData.getString("data");
		return reName;
	}

}
